<?php
//文件浏览
class FileAction extends Action{

	function init(){
		UserModel::isLogin(true); 
	} 

	function cmdIndex(){
		$dir=_PATH_;
		if (empty($_GET['dir']))$_GET['dir']="";

		if ($_GET['dir']=='..'){
			$dir=dirname(__FILE__)."/".$_GET['dir']."/";
		}else{
			$dir.="/".$_GET['dir']."/";
		}
		$dis='';	
		$dp = opendir(iconv("UTF-8", "GB2312", $dir));
		$fileArr=array();
		while (!false == $curFile = readdir($dp)){
			if ($_GET['dir']==""){
				if (!empty($curFile)&&$curFile!='..'&&$curFile!='.'){
					if (is_dir($dir."/".$curFile)) {
						if ($curFile==".."){
							$curFile=iconv("GB2312", "UTF-8", $curFile);
							$dis.= '<li><a href="?dir='. str_ireplace("//", "/",  str_replace("\\", "/",str_ireplace(realpath(_PATH_), "", realpath($dir."/../")))).'"><img src="'.PATH."/".Controller::$_C['SMARTY']['Templates_dir'].'3g/images/img/dir2.gif"/>'. $curFile.'</a></li>';
						}else{
							$curFile=iconv("GB2312", "UTF-8", $curFile);
							$dis.= '<li><a href="?dir='.urlencode(str_ireplace(realpath(_PATH_), "", realpath($dir)).$curFile).'"><img src="'.PATH."/".Controller::$_C['SMARTY']['Templates_dir'].'3g/images/img/dir.gif"/>'. $curFile.'</a></li>';
						}
					} else {
						$curFile=iconv("GB2312", "UTF-8", $curFile);
						$dis.= "<li><a href='".PATH.str_replace(realpath(_PATH_), "",  realpath($dir))."/".$curFile."'>".'<img src="'.PATH."/".Controller::$_C['SMARTY']['Templates_dir'].'3g/images/img/'.$this->getExt($dir.$curFile).'.gif"/>'.$curFile."</a></li>";
					}
				}
			}else{
				if (!empty($curFile)){
					if (is_dir($dir."/".$curFile)) {
						if ($curFile==".."){
							$curFile=iconv("GB2312", "UTF-8", $curFile);
							$dis.= '<li><a href="?dir='. str_ireplace("//", "/",  str_replace("\\", "/",str_ireplace(realpath(_PATH_), "", realpath($dir."/../")))).'"><img src="'.PATH."/".Controller::$_C['SMARTY']['Templates_dir'].'3g/images/img/dir2.gif"/>'. $curFile.'</a></li>';
						}else{
							$curFile=iconv("GB2312", "UTF-8", $curFile);
							$dis.= '<li><a href="?dir='.str_ireplace("//", "/",str_ireplace(_PATH_, "", $dir)).$curFile.'"><img src="'.PATH."/".Controller::$_C['SMARTY']['Templates_dir'].'3g/images/img/dir.gif"/>'. $curFile.'</a></li>';
						}
					} else { 
							$curFile=iconv("GB2312", "UTF-8", $curFile);
							$dis.= '<li><a href="'.PATH.str_replace(realpath(_PATH_), "",  realpath($dir))."/".$curFile.'"><img src="'.PATH."/".Controller::$_C['SMARTY']['Templates_dir'].'3g/images/img/'.$this->getExt($dir.$curFile).'.gif"/>'.$curFile."</a></li>";
					}
				}
			}
		}
		$this->assign("title", "");	
		$this->assign("dir",str_replace("//", "/",  $dir));
		$this->assign("dis", $dis);
		$this->display("File_List.htm");
	}
	
	function getSize($filename){
		if (!is_file($filename))return '0B';
		$size=filesize($filename);
		if ($size<1024){
			$return =$size."B";
		}elseif ($size<1024000){
			$return= ($size/1024)."KB";
		}elseif ($size<1024000000){
			$return= ($size/1024000)."MB";
		}else{
			$return =($size/1024000000)."GB";
		}
		return $return;
	}
	
	function getExt($filename){ 
		$img=array("php","gif","txt","htm","css","flash","jpg","js","mp3","rm","wmv","zip","png","rar",
		'swf','bmp','hlp','chm','doc','xls','pdf','ppt');
		if (!is_file($filename))return 'unknow';
		$t=explode(".", $filename);
		$ext = $t[count($t)-1];
			foreach ($img as $z=>$b){
				if ($ext==$b){
					return  $ext."\n";	
				}else{
					
				}
			}
		return 'unknow';
	}
}
?>